package com.xyj.dao.base.impl;

import com.xyj.dao.base.BaseDaoI;
import com.xyj.util.LoggerUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Created by 徐宇健 on 2015/2/11 0011.
 */
@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDaoI <T> {
    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }
    @Override
    public Serializable save(T t) {
        return getCurrentSession().save(t);
    }

    @Override
    public void update(T t) {
        getCurrentSession().update(t);
    }

    @Override
    public void delete(T t) {
        getCurrentSession().delete(t);
    }

    @Override
    public void saveOrUpdate(T t) {
        getCurrentSession().saveOrUpdate(t);
    }

    @Override
   public T get(String hql,Object[] objs) {
        //"from com.xyj.domain.TUser t where t.name =? and  t.pwd = ?"
      Query query =  getCurrentSession().createQuery(hql);
      if(objs!=null){
          for (int i = 0; i <objs.length ; i++) {
              query.setParameter(i,objs[i]);
          }
      }
      return (T) query.uniqueResult();
    }



    @Override
    public T get(String hql, Map<String, Object> params) {
        LoggerUtil.getLogger().info("hql>>> : "+hql);
        Query query =  getCurrentSession().createQuery(hql);
        for(String key:params.keySet()){
            query.setParameter(key,params.get(key));
        }
        return (T) query.uniqueResult();
    }

    @Override
    public List<T> find(String hql) {
        Query query =  getCurrentSession().createQuery(hql);
        return query.list();
    }

    @Override
    public List<T> find(String hql, Map<String, Object> params) {
        Query query =  getCurrentSession().createQuery(hql);
        if(params != null){
            for(String key:params.keySet()){
                query.setParameter(key,params.get(key));
            }
        }
        return query.list();
    }

    @Override
    public List<T> find(String hql, Map<String, Object> params, int startPage, int pageSize) {
        Query query =  getCurrentSession().createQuery(hql);
        for(String key:params.keySet()){
            query.setParameter(key,params.get(key));
        }
        return query.setFirstResult((startPage-1)*pageSize).setMaxResults(pageSize).list();
    }

    @Override
    public List<T> find(String hql, int startPage, int pageSize) {
        Query query =  getCurrentSession().createQuery(hql);
        return query.setFirstResult((startPage-1)*pageSize).setMaxResults(pageSize).list();
    }

    @Override
    public Long count(String hql) {
        Query query =  getCurrentSession().createQuery(hql);
        return (Long) query.uniqueResult();
    }

    @Override
    public Long count(String hql, Map<String, Object> params) {
        Query query =  getCurrentSession().createQuery(hql);
        for(String key:params.keySet()){
            query.setParameter(key,params.get(key));
        }
        return  (Long) query.uniqueResult();
    }


}
